package com.cskaoyan.tcp.exercise.first;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/*
        客户端键盘录入，服务器输出到控制台
 */
public class Server {

    public static void main(String[] args) throws IOException {

        // 1. 创建ServerSocket对象
        ServerSocket serverSocket = new ServerSocket(9090);

        // 2. accept 接收处理连接请求
        Socket socket = serverSocket.accept();

        //3. 获取输入流，读取发送端发送的数据
        InputStream in = socket.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);
        char[] chars = new char[1024];

        boolean flag = true;
        while (flag) {
            // 接收数据
            int count = reader.read(chars);
            String s = new String(chars, 0, count);
            System.out.println(s);
        }

        // 关闭socket
        socket.close();
        serverSocket.close();

    }
}
